package com.fr.spark;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author 付荣
 * @version 创建时间：2019年7月1日 下午5:05:00
 */
public class NaiveBayes {
    static int total = 0;
    static Map<String, Integer> categoryMap = new HashMap<>();
    static Map<String, Integer> districtMap = new HashMap<>();
    static Map<String, Integer> keywordMap = new HashMap<>();
    static  Map<String, Map<String, Integer>> districtCategorymap = new HashMap<>();
    static Map<String, Map<String, Integer>> keywordCategorymap = new HashMap<>();

    @BeforeClass
    public static void dataPrepare() throws IOException {

        // keyword district category count
        FileReader fr = new FileReader("E:\\test\\keyword_category.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = br.readLine();
        while (line != null) {
            String[] s = line.split("\t");
            int count = Integer.parseInt(s[3]);
            total += count;
            // 品类
            Integer tmp = categoryMap.get(s[2]);
            if (tmp == null) {
                tmp = 0;
            }
            tmp += count;
            categoryMap.put(s[2], tmp);
            // 出发地
            tmp = districtMap.get(s[1]);
            if (tmp == null) {
                tmp = 0;
            }
            tmp += count;
            districtMap.put(s[1], tmp);
            // 出发地+品类
            Map<String, Integer> tmpMap = districtCategorymap.get(s[1]);
            if (tmpMap == null) {
                tmpMap = new HashMap<>();
                districtCategorymap.put(s[1], tmpMap);
            }
            tmp = tmpMap.get(s[2]);
            if (tmp == null) {
                tmp = 0;
            }
            tmp += count;
            tmpMap.put(s[2], tmp);
            // 关键词
            tmp = keywordMap.get(s[0]);
            if (tmp == null) {
                tmp = 0;
            }
            tmp += count;
            keywordMap.put(s[0], tmp);
            tmpMap = keywordCategorymap.get(s[0]);
            if (tmpMap == null) {
                tmpMap = new HashMap<>();
                keywordCategorymap.put(s[0], tmpMap);
            }
            tmp = tmpMap.get(s[2]);
            if (tmp == null) {
                tmp = 0;
            }
            tmp += count;
            tmpMap.put(s[2], tmp);
            line = br.readLine();
        }
        br.close();
        fr.close();
    }

    @Test
    public void testList() {
        String keywords[] = {"上海", "北京", "成都", "西安", "南宁", "", "欢乐谷", "希尔顿", "欧洲", "三清山"};
        String districts[] = {"上海", "北京", "成都", "西安", "南宁", ""};
        for (String keyword : keywords) {
            Integer k = keywordMap.get(keyword);
            if (k == null) {
                System.out.println("keyword : " + keyword + "无统计结果");
                continue;
            }
            Map<String, Integer> kcMap = keywordCategorymap.get(keyword);
            if (kcMap == null) {
                System.out.println("keyword : " + keyword + "无统计结果");
                continue;
            }
            for (String district : districts) {
                Integer d = districtMap.get(district);
                if (d == null) {
                    System.out.println("district : " + district + "无统计结果");
                    continue;
                }
                Map<String, Integer> dcMap = districtCategorymap.get(district);
                if (dcMap == null) {
                    System.out.println("district : " + district + "无统计结果");
                    continue;
                }
                String c = null;
                double a = -1;
                for (Entry<String, Integer> entry : categoryMap.entrySet()) {
                    String category = entry.getKey();
                    Integer kc = kcMap.get(category);
                    Integer dc = dcMap.get(category);
                    if (kc == null) {
                        kc = 0;
                    }
                    if (dc == null) {
                        dc = 0;
                    }
                    double tmp = (double)(entry.getValue()) * kc * dc / d / k;
                    if (tmp > a) {
                        a = tmp;
                        c = category;
                    }
                }
                System.out.println("keyword:" + keyword + ",district:" + district + ",category:" + c);
            }
        }

    }

}
